# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=setuptools
pkgbase="python-${_realname}"
pkgname=("python3-${_realname}" "python2-${_realname}")
pkgver=40.0.0
pkgrel=1
pkgdesc="Easily download, build, install, upgrade, and uninstall Python packages"
arch=('any')
license=('PSF')
url="https://pypi.python.org/pypi/setuptools"
makedepends=('python3-packaging' 'python2-packaging' 'python3-appdirs' 'python2-appdirs' 'python3-pip'
             'python2-pip')
source=("${_realname}-${pkgver}.tar.gz::https://github.com/pypa/setuptools/archive/v${pkgver}.tar.gz")
sha512sums=('c0e956ea87cbb4608be6c76182113000997a986c10a8c44f24f329237b0b0ff1770a140bbd7d6dc4736e4e652d99637e3adb07a3dc080aa7597413c7d7dd503e')

prepare() {
  # Don't download and install deps
  sed -i '/pip.main(args)/d' setuptools-${pkgver}/bootstrap.py

  # Remove post-release tag since we are using stable tags
  sed -e '/tag_build = .post/d' \
      -e '/tag_date = 1/d' \
      -i setuptools-${pkgver}/setup.cfg

  cp -a setuptools-${pkgver}{,-py2}

  cd "${srcdir}"/setuptools-${pkgver}
  sed -i -e "s|^#\!.*/usr/bin/env python|#!/usr/bin/env python3|" setuptools/command/easy_install.py

  cd "${srcdir}"/setuptools-${pkgver}-py2
  sed -i -e "s|^#\!.*/usr/bin/env python|#!/usr/bin/env python2|" setuptools/command/easy_install.py
  sed -i -e "s|'pip'|'pip2'|" setuptools/tests/{test_develop.py,test_namespaces.py}

  export SETUPTOOLS_INSTALL_WINDOWS_SPECIFIC_FILES=0
}

build() {
  # Build python 3 module
  cd "${srcdir}"/setuptools-${pkgver}
  python3 bootstrap.py
  python3 setup.py build

  # Build python 2 module
  cd "${srcdir}"/setuptools-${pkgver}-py2
  python2 bootstrap.py
  python2 setup.py build
}

check() { (
  # Workaround UTF-8 tests by setting LC_CTYPE
  export LC_CTYPE=en_US.UTF-8

  # https://github.com/pypa/setuptools/pull/810
  export PYTHONDONTWRITEBYTECODE=1

  # Check python3 module
  cd "${srcdir}"/setuptools-${pkgver}
  python3 setup.py pytest

  # Check python2 module
  cd "${srcdir}"/setuptools-${pkgver}-py2
  python2 setup.py pytest
)}

package_python3-setuptools() {
  depends=('python3-packaging' 'python3-appdirs')
  provides=('python3-distribute')
  replaces=('python3-distribute')

  cd "${srcdir}"/setuptools-${pkgver}
  python3 setup.py install --prefix=/usr --root="${pkgdir}" --optimize=1 --skip-build
}

package_python2-setuptools() {
  depends=('python2-packaging' 'python2-appdirs')
  provides=('python2-distribute')
  replaces=('python2-distribute')

  cd "${srcdir}"/setuptools-${pkgver}-py2
  python2 setup.py install --prefix=/usr --root="${pkgdir}" --optimize=1 --skip-build
  rm "${pkgdir}"/usr/bin/easy_install
}
